<?php namespace Summer\Shop\Models;

use Config;
use Model;
use Winter\Storm\Support\Collection;
use RainLab\User\Models\User as UserModel;

/**
 * Model
 */
class OrderModel extends Model
{
    use \Winter\Storm\Database\Traits\Validation;
    

    /**
     * @var string The database table used by the model.
     */
    public $table = 'summer_shop_orders';
    protected $jsonable = ['user_address'];

    /**
     * @var array Validation rules
     */
    public $rules = [
    ];
    public $belongsTo = [
        'target' => ['RainLab\User\Models\User', 'key' => 'user_id','otherKey'=>'id'],
    ];
    public $hasMany = [
        'items' => ['Summer\Shop\Models\OrderItemModel','key'=>'order_id','otherKey'=>'id']
    ];
    /**
     * 用户搜索
     * @param $query
     * @param $field
     * @return bool
     */
    public function scopeTargetUser($query,$field)
    {
        $user=UserModel::where('userphone',$field)->orWhere('username',$field)->first();
        if(!$user){
            return false;
        }else{
            return $query->where('user_id',$user->id);
        }
    }

    /**
     * 订单状态
     * @return mixed
     */
    public function getStatusSlugOptions()
    {
        $order_status_slug=Config::get('summer.shop::order_status_slug');
        $collection = new Collection($order_status_slug);
        return $collection->pluck('title','slug')->toArray();
    }
}
